library(xcms)
## Loading required package: mzR
## Loading required package: Rcpp
## Loading required package: BiocGenerics
## Loading required package: parallel
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:parallel':
## 
##     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
##     clusterExport, clusterMap, parApply, parCapply, parLapply,
##     parLapplyLB, parRapply, parSapply, parSapplyLB
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, xtabs
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, append, as.data.frame, cbind, colnames,
##     do.call, duplicated, eval, evalq, Filter, Find, get, grep,
##     grepl, intersect, is.unsorted, lapply, lengths, Map, mapply,
##     match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
##     Position, rank, rbind, Reduce, rownames, sapply, setdiff,
##     sort, table, tapply, union, unique, unsplit, which, which.max,
##     which.min
## Loading required package: ProtGenerics
## Loading required package: Biobase
## Welcome to Bioconductor
## 
##     Vignettes contain introductory material; view with
##     'browseVignettes()'. To cite Bioconductor, see
##     'citation("Biobase")', and for packages 'citation("pkgname")'.
library(faahKO)

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:xcms':
## 
##     groups
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(reshape2)

cdfpath <- system.file("cdf", package = "faahKO")
cdffiles <- list.files(cdfpath, recursive = TRUE, full.names = TRUE)
xr<-xcmsRaw(cdffiles[1])
xr
## An "xcmsRaw" object with 1278 mass spectra
## 
## Time range: 2501.4-4499.8 seconds (41.7-75 minutes)
## Mass range: 200-600 m/z
## Intensity range: 70-1373180 
## 
## MSn data on  0  mass(es)
##  with  0  MSn spectra
## Profile method: bin 
## Profile step: 1 m/z (401 grid points from 200 to 600 m/z)
## 
## Memory usage: 10.7 MB
plotTIC(xr)

xr@tic
##    [1]  948152  954658  950875  943870  949634  965587  968803  964557
##    [9]  969358  964664  971634  971365  974217  978026  970441  972351
##   [17]  967410  961510  965924  961907  965915  965538  960962  962236
##   [25]  963604  963342  965389  955182  947886  945688  947762  949261
##   [33]  950104  950831  943956  942093  944989  941028  930184  923923
##   [41]  922735  922978  920220  919034  926627  932312  939707  939193
##   [49]  950585  988887 1012947 1052623 1089801 1121788 1153565 1182374
##   [57] 1203382 1213689 1206138 1192362 1179480 1168073 1159887 1143179
##   [65] 1130904 1108635 1090106 1079381 1073463 1063551 1056777 1058043
##   [73] 1048315 1029606 1016288 1012317 1010362 1008342 1007927 1001495
##   [81]  994241  991321  995081 1000696 1006012 1010445 1010192 1000548
##   [89] 1000251 1013063 1035840 1068247 1131977 1211678 1323509 1454893
##   [97] 1596095 1726614 1845832 1929584 1975660 1990756 1983879 1981681
##  [105] 1981225 2001352 2076263 2184020 2337732 2489592 2616192 2699532
##  [113] 2776014 2845992 2879776 2864635 2814306 2747112 2670893 2576134
##  [121] 2466354 2332135 2195738 2060420 1934017 1820978 1704743 1598196
##  [129] 1495144 1413206 1354217 1307148 1273195 1236117 1197277 1164971
##  [137] 1153781 1141773 1132081 1126910 1107519 1104597 1094940 1090685
##  [145] 1082949 1078664 1065321 1056660 1053121 1055837 1062535 1082841
##  [153] 1108271 1148762 1180745 1216048 1242786 1266753 1278982 1281879
##  [161] 1273977 1261918 1236184 1209667 1191196 1168379 1153621 1134224
##  [169] 1115367 1111315 1141234 1200931 1301340 1452604 1638248 1848593
##  [177] 2064119 2243219 2393347 2484020 2538763 2574788 2568901 2511377
##  [185] 2427208 2319780 2187203 2058017 1921906 1798203 1678016 1562513
##  [193] 1461224 1378079 1318078 1269855 1222825 1200244 1182833 1165732
##  [201] 1158537 1164549 1165669 1169317 1185052 1195401 1205060 1215649
##  [209] 1226407 1223247 1213606 1221738 1229800 1232470 1237469 1242425
##  [217] 1246680 1240525 1235645 1238741 1247128 1247666 1249171 1257258
##  [225] 1270229 1296567 1318313 1336171 1333280 1345191 1358078 1379915
##  [233] 1397397 1410733 1407732 1418112 1443059 1452214 1457244 1465325
##  [241] 1471362 1458500 1461502 1480420 1488575 1474581 1487778 1486055
##  [249] 1486331 1492078 1502957 1510785 1516092 1529547 1540462 1545746
##  [257] 1545159 1540285 1559794 1573836 1595906 1621487 1634816 1668121
##  [265] 1685742 1719225 1741881 1755954 1769490 1774350 1791580 1784915
##  [273] 1771809 1746439 1728918 1704181 1699212 1665394 1650910 1637394
##  [281] 1607237 1591654 1592812 1594733 1591871 1587330 1577106 1568270
##  [289] 1564406 1569054 1566882 1573750 1557696 1546669 1548667 1552492
##  [297] 1542310 1529498 1518640 1507836 1500306 1499182 1487030 1470177
##  [305] 1462480 1449739 1452404 1445392 1439811 1440987 1433821 1425530
##  [313] 1417061 1414153 1418914 1430596 1435735 1437346 1445789 1431984
##  [321] 1435822 1446139 1440606 1439012 1426008 1431290 1432261 1431220
##  [329] 1419388 1416158 1410527 1410297 1421765 1423667 1424063 1410890
##  [337] 1398731 1384351 1369588 1362710 1347777 1337880 1322352 1318862
##  [345] 1306935 1306680 1310284 1313143 1316656 1324811 1340222 1350161
##  [353] 1368371 1383146 1409618 1420724 1447455 1466349 1474997 1472526
##  [361] 1475258 1467224 1466553 1452237 1434372 1418788 1414168 1408275
##  [369] 1390868 1380443 1370014 1356349 1351401 1347061 1335776 1329321
##  [377] 1321528 1320879 1319415 1317767 1315184 1305109 1293650 1291085
##  [385] 1289964 1287638 1289619 1280679 1267864 1269163 1264599 1272310
##  [393] 1267168 1266685 1269294 1270465 1270621 1264610 1264432 1272707
##  [401] 1274919 1265394 1265655 1268532 1273274 1275777 1285020 1295752
##  [409] 1327596 1387673 1489675 1672065 1925216 2220211 2561554 2899077
##  [417] 3183572 3399013 3534682 3655550 3744633 3792791 3799214 3770414
##  [425] 3734659 3690353 3620137 3525887 3427586 3353456 3282923 3194111
##  [433] 3112582 3050411 2977495 2903657 2832837 2767536 2705990 2653695
##  [441] 2585103 2515463 2435910 2363101 2302676 2246279 2198579 2142911
##  [449] 2091944 2032856 1988677 1966529 1933520 1910040 1888659 1871381
##  [457] 1862669 1844917 1846045 1839911 1836259 1851901 1873614 1896584
##  [465] 1902852 1906842 1908996 1911053 1913476 1924928 1940186 1970999
##  [473] 2003220 2057184 2086397 2131621 2159541 2175560 2180855 2188392
##  [481] 2156315 2118394 2086059 2061342 2048114 2046219 2057868 2110345
##  [489] 2188315 2300619 2409399 2503341 2578732 2623811 2644581 2647912
##  [497] 2643534 2633881 2615308 2583191 2566776 2578897 2637474 2725487
##  [505] 2856667 3009459 3180441 3383663 3581645 3771287 3935906 4077654
##  [513] 4189880 4257108 4289336 4326073 4363576 4399575 4420037 4416479
##  [521] 4409271 4402916 4363976 4296614 4189189 4089764 4004224 3936341
##  [529] 3906573 3932152 3978812 4086272 4216894 4355958 4469479 4547873
##  [537] 4607500 4634347 4625075 4599847 4546050 4464662 4340748 4198104
##  [545] 4063318 3941792 3824316 3704788 3585367 3460679 3344363 3253713
##  [553] 3199488 3205174 3272527 3391207 3552053 3710168 3853115 3946218
##  [561] 4009762 4066949 4125008 4164565 4173423 4164368 4149586 4105863
##  [569] 4042014 3982422 3943186 3889747 3833539 3787667 3750715 3737703
##  [577] 3698772 3647732 3612343 3576120 3532291 3475672 3421787 3345433
##  [585] 3266252 3196057 3120181 3042148 2956991 2889870 2830928 2792445
##  [593] 2758923 2731737 2712933 2704960 2702942 2719255 2744317 2786846
##  [601] 2837578 2889492 2905602 2940953 2971147 3022174 3039224 3044218
##  [609] 3043948 3021154 2986846 2960530 2931693 2905957 2858470 2827817
##  [617] 2777815 2743104 2717679 2706667 2692890 2673893 2649744 2623651
##  [625] 2604775 2580190 2576467 2573832 2567970 2576394 2634331 2663412
##  [633] 2673523 2725807 2780814 2841104 2891148 2994116 3149356 3356068
##  [641] 3558091 3730448 3876774 4008499 4100065 4137212 4146445 4155521
##  [649] 4144890 4126892 4117441 4102312 4075047 4039427 4029589 4000364
##  [657] 3941099 3864471 3809498 3765761 3715432 3644832 3562377 3471036
##  [665] 3397884 3305360 3229110 3163499 3102655 3064961 3021848 2988678
##  [673] 2967299 2954219 2943733 2929211 2918452 2914277 2918752 2929288
##  [681] 2954290 2980152 3003550 3027383 3039737 3052933 3069215 3092545
##  [689] 3111335 3119313 3126802 3131407 3124690 3111021 3099996 3083754
##  [697] 3062884 3029085 2992268 2968817 2940242 2908796 2891124 2875096
##  [705] 2872865 2894915 2922161 2956186 3020915 3102843 3197869 3292958
##  [713] 3358219 3410276 3425282 3404298 3360022 3306175 3244718 3188587
##  [721] 3137269 3110965 3098542 3106617 3104778 3105047 3098997 3110945
##  [729] 3154823 3207575 3288710 3377831 3493514 3622504 3747468 3884041
##  [737] 4030853 4117974 4216706 4297713 4319800 4340639 4300571 4228095
##  [745] 4112744 3967090 3851069 3726901 3598253 3514926 3421937 3344681
##  [753] 3247316 3141637 3072042 3010917 2981364 2931631 2872730 2856452
##  [761] 2826274 2811758 2826135 2853480 2829714 2811595 2810306 2792254
##  [769] 2775147 2730241 2688709 2653496 2597134 2541317 2493747 2423760
##  [777] 2369627 2315660 2271354 2244406 2208706 2165758 2131858 2084505
##  [785] 2034466 1994813 1959685 1945968 1943996 1952187 1955029 1954597
##  [793] 1952869 1951186 1949962 1945740 1935313 1921166 1910021 1902289
##  [801] 1886374 1857205 1830948 1791583 1760211 1732374 1706762 1689288
##  [809] 1669498 1670197 1680873 1681462 1688099 1694698 1701549 1732812
##  [817] 1770348 1795806 1798572 1795862 1788219 1787413 1782621 1779583
##  [825] 1767634 1749198 1713618 1688633 1673848 1680083 1683670 1698949
##  [833] 1726022 1758340 1802901 1862555 1923836 1991965 2070876 2148028
##  [841] 2217256 2274495 2307085 2319059 2318841 2313629 2313652 2323478
##  [849] 2339036 2352273 2357951 2347212 2323504 2294203 2265035 2233822
##  [857] 2197123 2164606 2136143 2108040 2085426 2076345 2078273 2099248
##  [865] 2127957 2162242 2190449 2212008 2220029 2200050 2161932 2124233
##  [873] 2120693 2143089 2178083 2213169 2239546 2251881 2253806 2257554
##  [881] 2268315 2265459 2259565 2258278 2264114 2250350 2207695 2152133
##  [889] 2119813 2105645 2103384 2107703 2087565 2049436 2026100 2034285
##  [897] 2057817 2075273 2075565 2058579 2051116 2052822 2033323 2011625
##  [905] 2013490 2035047 2041788 2028113 2014793 2011168 2012942 2012433
##  [913] 1999993 1973221 1949842 1939031 1936301 1926400 1922731 1908002
##  [921] 1889714 1859886 1838434 1819283 1804334 1798277 1802582 1793352
##  [929] 1786508 1763553 1759723 1750716 1737732 1735163 1742355 1744645
##  [937] 1748562 1746883 1761666 1775387 1788628 1786130 1785323 1799774
##  [945] 1811834 1811051 1802536 1794056 1784788 1786431 1765935 1749149
##  [953] 1743696 1744057 1747921 1734278 1722104 1715808 1709440 1703933
##  [961] 1694451 1686106 1683122 1681523 1679190 1672262 1669629 1670130
##  [969] 1673267 1680515 1691405 1700136 1712220 1711876 1706183 1701413
##  [977] 1710983 1719643 1726023 1718751 1716108 1729049 1752903 1782874
##  [985] 1814469 1844167 1885509 1947868 2026755 2114460 2212317 2322109
##  [993] 2441848 2545429 2610041 2633552 2637636 2646454 2661609 2666096
## [1001] 2650359 2636007 2623724 2616778 2604329 2593868 2576468 2570849
## [1009] 2577175 2596545 2631177 2672064 2709265 2739385 2770737 2787060
## [1017] 2791674 2784402 2773129 2754182 2728527 2689325 2651845 2617581
## [1025] 2595398 2589494 2612409 2668643 2755068 2849776 2935145 2994745
## [1033] 3024610 3041911 3032605 3011983 2971748 2911739 2832265 2747687
## [1041] 2660795 2583417 2499426 2383636 2221057 2011419 1785047 1576868
## [1049] 1389995 1227663 1092690  990658  917890  862065  827533  822187
## [1057]  838078  874708  921570  971302 1023679 1087242 1161856 1231503
## [1065] 1268068 1278065 1283304 1274596 1239646 1179942 1089704  992996
## [1073]  901869  839908  806691  790456  788429  798808  822139  851122
## [1081]  882609  917075  959708 1002758 1038929 1063770 1066993 1043521
## [1089]  997989  941175  887703  846285  820738  802354  772209  727521
## [1097]  666423  602503  536473  471709  411447  358031  313912  279419
## [1105]  249921  223292  200642  182122  165472  151513  133247  125548
## [1113]  125039  128482  136752  146306  158895  171257  178195  183319
## [1121]  187803  189321  185411  179032  173595  168559  165674  163804
## [1129]  164210  169931  176851  184991  202139  237677  290975  347405
## [1137]  397625  441460  478102  505237  519291  520032  502132  474585
## [1145]  439033  403486  367058  332441  299054  271361  250329  237356
## [1153]  229807  229676  232399  241681  246510  250882  248035  240336
## [1161]  228821  213726  199433  185132  174501  167658  163386  162052
## [1169]  161110  160466  163481  164364  167038  170323  174543  180619
## [1177]  187405  195565  201388  203701  207552  214127  220112  224087
## [1185]  229035  235994  241153  243508  245877  248773  251631  254062
## [1193]  253961  251228  248855  252043  258329  264652  267781  269081
## [1201]  270533  274900  278317  279322  281004  285454  289244  292366
## [1209]  293802  298774  310358  324547  335360  340057  350054  367446
## [1217]  388942  411148  436543  464298  495407  522991  542339  559298
## [1225]  581797  610969  637940  658849  661923  657429  648016  640075
## [1233]  631756  625106  621901  615456  606095  595305  587658  580470
## [1241]  583166  595588  614471  633097  651229  665310  683337  708157
## [1249]  737405  763035  780251  790361  794650  792521  785598  768734
## [1257]  752433  724891  698871  669392  643353  615424  584725  563652
## [1265]  547730  531399  518629  504554  494902  488970  482959  476756
## [1273]  472473  466554  464119  469922  483393  499115
plot(xr@scantime,xr@tic)

plot(xr@scantime,xr@tic, type = 'l')

retention_time_ms<-xr@scantime
total_ion_chromatogram<-xr@tic
chrom <- data.frame(retention_time_ms,total_ion_chromatogram)
write.csv(chrom, 'chrom.csv', row.names = FALSE)

gg_chrom<-ggplot(chrom, aes(chrom$retention_time_ms)) + 
  geom_line(aes(y = chrom$total_ion_chromatogram)) 

gg_chrom

ggplotly(gg_chrom)
spec<-read.csv('chrom.csv',header = T)

gg_chrom<-ggplot(spec, aes(spec$retention_time_ms)) + 
  geom_line(aes(y = spec$total_ion_chromatogram)) 

library(dygraphs)

dygraph(spec) %>%
  dyRangeSelector()